package com.lark.component;

import com.lark.utils.Lazy;
import com.lark.utils.ResourcesUtil;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import lombok.Getter;
import org.kordamp.ikonli.javafx.FontIcon;

/**
 * 导航组件
 */
public class NavItem extends HBox {

    private static final String STYLE_SHEET = ResourcesUtil.convertFilePath("static/css/nav.css");

    /**
     * 图标
     */
    private final Label iconLabel = new Label();

    /**
     * 标题
     */
    private final Label titleLabel = new Label();

    /**
     * 内容界面
     */
    @Getter
    private final Lazy<? extends Node> content;

    public NavItem(FontIcon fontIcon, String title, Lazy<? extends Node> content) {
        this.iconLabel.setGraphic(fontIcon);
        this.titleLabel.setText(title);
        this.content = content;
        initialize();
    }

    private void initialize() {
        getChildren().addAll(iconLabel, titleLabel);

        // 标题占满剩余空间
        titleLabel.setMaxSize(Double.MAX_VALUE, USE_PREF_SIZE);
        HBox.setHgrow(titleLabel, Priority.ALWAYS);

        getStyleClass().add("nav-item");
        iconLabel.getStyleClass().add("icon-label");
        titleLabel.getStyleClass().add("name-label");
    }

    public Label getTitle() {
        return titleLabel;
    }

    public void remove() {
        getChildren().remove(titleLabel);
    }

    public void add() {
        getChildren().add(titleLabel);
    }

    @Override
    public String getUserAgentStylesheet() {
        return STYLE_SHEET;
    }
}
